from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base

DATABASE_URL = "mysql+pymysql://root:root@localhost:3306/fastapi"

engine = create_engine(
    DATABASE_URL,
    # echo=True,
    pool_pre_ping=True,
    pool_recycle=1800,
    pool_size=60,
    max_overflow=20,  # 默认是 10

)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()


def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
